package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.AttackDetectionAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author:lxb
 * @Package
 * @date:2021/4/15
 */
@Service
@AttrHandler(AttackDetectionAttrHandler.class)
public class AttackDetectionAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Override
    public void handle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {
        if("210004106".equals(kiteAttr.getFieldName())) {
            //攻击检测为是   这四个属性必填
            //210004149 攻击监测IPv4
            //210004108	/64及以上每IPV6地址段
            //210004109	/56-/64(含/56)每IPV6地址段
            //210004111	/48-/56(含/48)每IPV6地址段

            // 新增附加商品属性个数、编码不同，直接取除 210004106 外的剩余属性
            List<KiteAttr> attrList = kiteInst.listAttrs().stream().filter(attr -> !KiteStringUtils.isEqual(attr.getFieldName(), kiteAttr.getFieldName()) && KiteStringUtils.isEqual(attr.getTableName(), "PROD_INST_FUNC")).collect(Collectors.toList());
            if ("1_0000106".equals(kiteAttr.getValue())) {
                attrList.forEach(attr -> {
                    if (Objects.nonNull(attr)) {
                        attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                });
            } else {
                attrList.forEach(attr -> {
                    if (Objects.nonNull(attr)) {
                        attr.setValue("");
                        attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                        attr.setIsNull(KeyConsts.IFTRUE_T);
                    }
                });
            }
            /*
            KiteAttr attackDetection = kiteInst.getAttr("210004149");
            KiteAttr ipv1 = kiteInst.getAttr("210004108");
            KiteAttr ipv2 = kiteInst.getAttr("210004109");
            KiteAttr ipv3 = kiteInst.getAttr("210004111");
            if ("1_0000106".equals(kiteAttr.getValue())) {
                if (Objects.nonNull(ipv1)) {
                    //ipv1.setIsNull(KeyConsts.IFTRUE_F);
                    ipv1.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                }
                if (Objects.nonNull(ipv2)) {
                    //ipv2.setIsNull(KeyConsts.IFTRUE_F);
                    ipv2.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                }
                if (Objects.nonNull(ipv3)) {
                    //ipv3.setIsNull(KeyConsts.IFTRUE_F);
                    ipv3.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                }
                if (Objects.nonNull(attackDetection)) {
                    //attackDetection.setIsNull(KeyConsts.IFTRUE_F);
                    attackDetection.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                }
            } else {
                ipv1.setValue("");
                ipv1.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                ipv1.setIsNull(KeyConsts.IFTRUE_T);
                ipv2.setValue("");
                ipv2.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                ipv2.setIsNull(KeyConsts.IFTRUE_T);
                ipv3.setValue("");
                ipv3.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                ipv3.setIsNull(KeyConsts.IFTRUE_T);
                attackDetection.setValue("");
                attackDetection.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                attackDetection.setIsNull(KeyConsts.IFTRUE_T);
            }
            */
        }

    }

    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {

    }
}
